<?php
/**
 * 客户基本信息上传
 */
namespace app\adminweb\controller;
use \think\Controller;
use \think\Validate;
header("Access-Control-Allow-Origin: * ");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
class Account extends Pre
{
    //添加当前客户信息
    public function inserts(){
        $account = new \app\common\model\Account();
        return $account -> inserts($this -> loginInfo);
    }
    //删除当前账户信息
    public function dels(){
        $account = new \app\common\model\Account();
        return $account -> dels();
    }
    //查找当前自己开的账户
    public function select(){
        $data = input('param.');
        $login = $this -> loginInfo;
        //查找有多少条数据
        if($data['key']){
            $data['key'] = trim($data['key']);
            $findMap[] = ['name|telephone','like',"%$data[key]%"];
            $data['page'] = 1;
        }
        $map[] = ['a_id','eq',$login['id']];
        if($data['mark'] && $data['mark'] !== 'ziji'){
            $allAccount = \app\common\model\Record::where($map) -> column('b_id');
            while (!empty($b_id = \app\common\model\Record::where($map)->column('b_id'))){
                $map = [];
                $allAccount = array_unique(array_filter(array_merge($allAccount,$b_id)));
                $map[] = ['a_id','in',$b_id];
            }
            if($data['mark'] == "quanbu"){
                $mapOne[] = ['a_id','in',$allAccount];
                $result2 = \app\common\model\Record::where($mapOne)
                    -> order('id Desc')->column('b_id');
            }else{
                $mapTwo[] = ['role','eq',$data['mark']];
                $role = \app\common\model\Account::where($mapTwo) -> column('id');
                $result2 = array_intersect($allAccount,$role);
            }
        }else{
            if($data['id']){
                $sign = \app\common\model\Record::where('a_id',$data['id']) -> column('b_id');
                $number = \app\common\model\Account::where('id','in',$sign)
                    -> field('id,name') -> select();
                $findM[] = ['a_id','eq',$data['id']];
            }else {
                $sign = \app\common\model\Record::where('a_id',$login['id']) -> column('b_id');
                $number = \app\common\model\Account::where('id','in',$sign)
                    -> field('id,name') -> select();
                $findM[] = ['a_id', 'eq', $login['id']];
            }
            $result2 = \app\common\model\Record::where($findM)
                -> order('id Desc')-> column('b_id');
        }
        foreach ($result2 as $ve){
            $role = \app\common\model\Account::get($ve);
            if(!in_array($role['role'],array(15,24))){
                $tmp1[] = $ve;
            }
        }
        $findMap[] = ['id','in',$tmp1];
        $result = \app\common\model\Account::where($findMap)
                -> order('id Desc') -> page($data['page'],$data['limit']) ->select();
        foreach ($result as $k=> $value){
            if($value['distribute']){
                $bute = \app\common\model\Account::get($value['distribute']);
                $result[$k]['distribute'] = $bute['name'];
            }
            if($value['style'] == null)
                $value['style'] = '';
            $value['style1'] = ltrim($value['style'],',');
            if(!$value['project'])
                $value['project'] = '';
            if($value['project'] !== 0){
                $xiangmu = explode(',',$value['project']);
                foreach($xiangmu as $ke => $val){
                    $name = \app\common\model\Building::get($val);
                    if($name){
                        $name ->toArray();
                        $tm = $tm.','.$name['name'];
                    }
                }
            }
            $result[$k]['project1'] = $value['project'];
            $result[$k]['project'] = ltrim($tm,',');
            $result[$k]['time'] = date('Y-m-d H:i:s',$value['time']);
            $role1 = \app\common\model\Role::get($value['role']);
            $result[$k]['role'] = $role1['rolename'];
            unset($tm);
            unset($strr);
        }
        if(!$data['key']){
            $count = count($tmp1);
        }else{
            $count = \app\common\model\Account::where($findMap) -> count();
        }
        $array = array('code'=>0,'msg' =>'','count'=>$count,'data'=>$result,'number'=>$number);
        return $array;
    }
    //查找自己的上级
    public function choose(){
        $data = input('param.');
        $data['id'] = 6;
        $map[] = ['b_id','eq',$data['id']];
        while (!empty($b_id = \app\common\model\Record::where($map)->find())){
            $map = [];
            $allAccount[] = $b_id['a_id'];
            $map[] = ['b_id','in',$b_id['a_id']];
        }
        sort($allAccount);
        if($allAccount){
            foreach ($allAccount as $value){
                $mapin[] = ['id','eq',$value];
                $result1 = \app\common\model\Account::where($mapin) -> find();
                $result[] = $result1->toArray();
                unset($mapin);
            }

        }
        $array = array('code'=>0,'msg' =>'','count'=>count($result),'data'=>$result);
        return $array;
    }
    //四个选择框传值
    public function data(){
        $loginInfo = $this -> loginInfo;
        //楼盘名
        $building = \app\common\model\Building::column('company');
        foreach ($building as $va){
            if(!in_array($va,$tmp)){
                $tmp[] = $va;
            }
        }
        foreach ($tmp as $va){
            $map5[] = ['company','eq',$va];
            $project = \app\common\model\Building::where($map5)
                        -> field('id,name') -> select() -> toArray();
            $tmp2[] = $project;
            unset($map5);
        }
        foreach ($tmp2 as $val){
            foreach ($val as $valu){
                $wang[] = $valu;
            }
        }
        foreach ($wang as $value){
            $na = array('project' => $value['name']);
            $project1 = array_merge($value,$na);
            $tmp1[] = $project1;
        }
        //功能
        $arra = array(3,11,12,13,21,30);
        if(in_array($loginInfo['role'],$arra)){
            $map1[] = ['sign','eq',1];
        }else{
            $map1[] = ['sign','eq',2];
        }
        $function = \app\common\model\Gongneng::where($map1) -> select() -> toArray();
        //职位
        $role = $loginInfo['role'];
        $map2['id'] = array('eq',$role);
        $position = \app\common\model\Role::where($map2) -> find();
        $power = $position['power'];
        if($power){
            $tmp = explode(',',$power);
            foreach ($tmp as $value){
                $map3['id'] = array('eq',$value);
                $result2 = \app\common\model\Role::where($map3) -> find() -> toArray();
                $result = \app\common\model\Role::where($map3) -> find();
                if($result)
                    $info = explode(',',$result['word']);
                $array = array('word'=> $info);
                $result1 = array_merge($result2,$array);
                $result3[] = $result1;
            }
        }else{
            $result3 = array();
        }
        //分销总
        $mapinfo[] = ['role','eq',20];
        $distribute = \app\common\model\Account::where($mapinfo) -> field('id,name')
                    -> select();
        if($distribute)
            $distribute -> toArray();
        //当前自己的角色下的所有拓展人
        $allAccount = array();
        $findRecord[] = ['a_id','eq',$loginInfo['id']];
        while (!empty($b_id = \app\common\model\Record::where($findRecord)->column('b_id'))){
            $findRecord = [];
            $allAccount = array_filter(array_merge($allAccount,$b_id));
            $findRecord[] = ['a_id','in',$b_id];
        }
        foreach ($allAccount as $ve){
            $res = \app\common\model\Account::where('id',$ve)
                        -> field('id,name,role') ->find();
            if($res){
                $resu1[] = $res->toArray();
            }
        }
        foreach ($resu1 as $vu){
                if(!in_array($vu['role'],array(15,24))){
                        $resu3[] = $vu;
                }
        }
        //查找我下属所有的职位
        $allAccount1 = array();
        $mapTwo[] = ['a_id','eq',$loginInfo['id']];
        while (!empty($b_id1 = \app\common\model\Record::where($mapTwo)->column('b_id'))){
            $mapTwo = [];
            $allAccount1 = array_filter(array_merge($allAccount1,$b_id1));
            $mapTwo[] = ['a_id','in',$b_id1];
        }
        $mapOne[] = ['id','in',$allAccount1];
        $zhiwei = \app\common\model\Account::where($mapOne) -> column('role');
        $tmpInfo = array_unique($zhiwei);
        $tmpTwo[] = ['id',"in",$tmpInfo];
        $zhiweiOne = \app\common\model\Role::where($tmpTwo)->select();
        $resu2 = array_merge(array(['id'=>0,'name'=>'市场部']),$resu3);
        $array = array('project' => $tmp1,'position' => $result3,
                       'distribute'=>$distribute,'gongneng' => $function,
                        'expand' => $resu2,'zhiwei'=>$zhiweiOne);
        return $array;
    }
    //公司管理页面
    public function manage(){
        $data = input('param.');
        if($data['key']){
            $data['key'] = trim($data['key']);
            $map1[] = ['name|telephone','like',"%$data[key]%"];
            $data['page'] = 1 ;
        }
        $loginInfo = $this -> loginInfo;
        $map[] = ['a_id','eq',$loginInfo['id']];
        $b_id = \app\common\model\Record::where($map) -> column('b_id');
        foreach ($b_id as $va){
            $res = \app\common\model\Account::get($va);
            if(in_array($res['role'],array(15,24))){
                    $bid[] = $va;
            }
        }
        $map1[] = ['id','in',$bid];
        $result = \app\common\model\Account::where($map1) ->order('id DESC')
                    -> limit(($data['page']-1)*$data['limit'],$data['limit'])
                    ->select();
        if($result){
            $result-> toArray();
            foreach ($result as $value){
                $value['project1'] = $value['project'];
                $arr = explode(',',$value['project']);
                $tm = '';
                if(count($arr) !== 0){
                    for ($i=0;$i<count($arr);$i++){
                        $id = $arr[$i];
                        $building = \app\common\model\Building::get($id);
                        $tm = $tm.','.$building['name'];
                    }
                }
                $value['project'] = ltrim($tm,',');
                $value['time'] = date('Y-m-d H:i:s',$value['time']);
                if($value['distribute'] !== null){
                    if(intval($value['distribute']) !== 0){
                        $name = \app\common\model\Account::where('id',$value['distribute'])
                            -> field('name') -> find();
                        $value['distribute1'] = $name['name'];
                    }elseif($value['distribute'] == ''){
                        $value['distribute1'] = '';
                    }else{
                        $value['distribute1'] = '市场部';
                    }
                }else{
                    $value['distribute1'] = '';
                }
                $tmp[] = $value->toArray();
            }
        }
        $array = array('code'=>0,'msg' =>'','count'=>count($tmp),'data'=>$tmp);
        return $array;
    }
    //登录界面账号显示
    public function user(){
        $login = $this -> loginInfo;
        $fun = explode(',' ,$login['fun']);
        $action = \app\common\model\Gongneng::where('id','in',$fun) -> select();
        if($action){
            $action -> toArray();
            foreach ($action as $value){
                $array[] = $value['fun'];
            }
        }else{
            $array = array();
        }
        $login['role1'] = $login['role'];
        $result = \app\common\model\Role::where('id',$login['role']) -> find();
        $login['role'] = $result['rolename'];
        return json([
            'login' => $login,
            'fun' => $array,
        ]);
    }
    //展厅列表页
    public function zhanting(){
        $data = input('param.');
        $loginInfo = $this -> loginInfo;
        if($data['key']){
            $data['key'] = trim($data['key']);
            $map[] = ['name|telephone','like',"%$data[key]%"];
            $data['page'] = 1;
        }

        $map[] = ['distribute','eq',$loginInfo['id']];
        $result = \app\common\model\Account::where($map)-> with('buildingInfo')
                    ->page($data['page'],$data['limit'])
                    -> select();
        foreach ($result as $value){
            $value['project'] = $value['building_info']['name'];
        }
        $array = array('code'=>0,'msg' =>'','count'=>count($result),'data'=>$result);
        return $array;
    }
    //测试导入表格
    public function excle(){
        return view();
    }
}